package com.lyp.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lyp.crm.dao.RolesMapper;
import com.lyp.crm.dao.UserMapper;
import com.lyp.crm.entity.Fenye;
import com.lyp.crm.entity.Roles;
import com.lyp.crm.entity.Rolesmodules;
import com.lyp.crm.entity.Userroles;
@Service
public class RolesServiceImp implements RolesService {
	
	@Autowired
	private RolesMapper rolesMapper;
	@Autowired
	private Fenye<Roles> fenye;
	
	
	private UserMapper usermapper;
	@Override
	public Fenye<Roles> selectRoles(Fenye<Roles> fenye) {
		List<Roles> rows = rolesMapper.selectRoles(fenye);
		Integer total = rolesMapper.selectCountRoles(fenye);
		fenye.setRows(rows);
		fenye.setTotal(total);
		// TODO Auto-generated method stub
		return fenye;
	}

	@Override
	public Integer deleteRoles(Integer uroid) {
		// TODO Auto-generated method stub
		/*Integer selectuserroles = usermapper.selectuserroles(uroid);
		System.out.println(selectuserroles);
		if (selectuserroles==0) {*/
			return rolesMapper.deleteRoles(uroid);
	/*	}else{
			return	-1;
		}*/
		
	}

	@Override
	public Integer updateRoles(Roles roles) {
		// TODO Auto-generated method stub\
		Integer updateRoles = rolesMapper.updateRoles(roles);
		return updateRoles;
	}

	@Override
	public List<Roles> selectRoleslist(Roles roles) {
		// TODO Auto-generated method stub
		return rolesMapper.selectRoleslist(roles);
	}

	@Override
	public List<Roles> myRoleslist(Integer uid) {
		// TODO Auto-generated method stub
		return rolesMapper.myRoleslist(uid);
	}

	@Override
	public Integer selectUserroles(Userroles userroles) {
		// TODO Auto-generated method stub
		return rolesMapper.selectUserroles(userroles);
	}

	@Override
	public Integer insertUserroles(Userroles userroles) {
		// TODO Auto-generated method stub
		return rolesMapper.insertUserroles(userroles);
	}

	@Override
	public Integer deleteUserroles(Integer uuid) {
		// TODO Auto-generated method stub
		return rolesMapper.deleteUserroles(uuid);
	}

	@Override
	public Integer insertRoles(Roles roles) {
			return rolesMapper.insertRoles(roles);
	}

	@Override
	public Integer selectinsertRname(Roles roles) {
		// TODO Auto-generated method stub
		return rolesMapper.selectinsertRname(roles);
	}

	@Override
	public Integer selectdelByRrid(Integer rid) {
		return rolesMapper.selectdelByRrid(rid);
	}
	@Override
	public Integer InsertRolem(String rmmid,Integer rmrid) {
		// TODO Auto-generated method stub
		if(rmmid!=null && rmmid!="") {
			String[] split = rmmid.split(",");
			List<Rolesmodules> list = new ArrayList<Rolesmodules>();
			for (int i = 0; i < split.length; i++) {
				Rolesmodules jm = new Rolesmodules();
				jm.setRmmid(Integer.parseInt(split[i]));
				jm.setRmrid(rmrid);
				list.add(jm);
			}
			Integer a = rolesMapper.deleteRoleModule(rmrid);
			if (a >= 0) {
				rolesMapper.insertRoleModule(list);
			}
		}else {
			rolesMapper.deleteRoleModule(rmrid);
		}
		
	return 1;
	}
}
